package IO;

import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:扫描指定目录，并找到名称中包含指定字符的所有普通文件（不包含目录），并且后续询问用户是否要删除该文件
 * User: 86185
 * Date: 2023-11-29
 * Time: 8:35
 */
public class Demo11 {
    public static void main(String[] args) {
        System.out.println("请输入要搜索的根目录");
        Scanner scanner = new Scanner(System.in);
        String path = scanner.next();
        System.out.println("请输入要删除的关键词");
        String word = scanner.next();
        File root = new File(path);
        if (!root.isDirectory()) {
            System.out.println("非法输入");
            return;
        }
        scanDir(new File(path), word);
    }

    public static void scanDir(File curDir, String word) {
        File[] files = curDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        for (File f :
                files) {
            if (f.isFile()) {
                dealFile(f, word);
            } else {
                scanDir(f, word);
            }
        }
    }
    public static void dealFile(File file, String word) {
        if(!file.getName().contains(word)) {
            return;
        }
        System.out.println("是否要删除" + file.getName() + "?y/n");
        Scanner scanner = new Scanner(System.in);
        String ans = scanner.next();
        if (ans.equals("y")) {
            file.delete();
            System.out.println("删除成功");
        }
    }
}
